package com.example.myapplication10;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    private void requestImg(final URL imgUrl)
    {
        new Thread(new Runnable() {
            @Override
            public void run() {
                Bitmap bitmap = null;
                try {
                    //
                    bitmap = BitmapFactory.decodeStream(imgUrl.openStream());
                    showImg(bitmap);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
    private void showImg(final Bitmap bitmap){
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                ImageView imageView=findViewById(R.id.img1);
                imageView.setImageBitmap(bitmap);
            }
        });
    }
    public void gotochat(View view) {
        Intent intent=new Intent(MainActivity.this,chat.class);
        startActivity(intent);
    }

    public void showpic(View view) {
        URL url = null;
        EditText text=findViewById(R.id.url);
        try {
            //url = new URL("https://tse1-mm.cn.bing.net/th/id/R-C.1caa5dbdb2718cc63638be218954b560?rik=HeNH91rEwcqm1Q&riu=http%3a%2f%2f5b0988e595225.cdn.sohucs.com%2fimages%2f20190117%2fb6050564892f4c17ac9b21deb3987982.jpeg&ehk=A%2fH0E2BgMs7%2bKrVUJ35lz09neCSCfqxMChTwSgj2hBc%3d&risl=&pid=ImgRaw&r=0");
            url=new URL(text.getText().toString());
            requestImg(url);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }
}